package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author 花鸟の虫鱼
 * @CreateDate 21/9/5 21:18
 * @Version 1.0
 * @Description TODO
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        //  计算当前页页码
        page = page == 0 ? 1 : page;
        //  计算从哪一页开始
        int offSet = (page - 1) * rows;
        Map<String, Object> map = new HashMap<>();
        //  查询客户信息放入列表
        List<Customer> customerList = customerDao.getCustomerList(offSet, rows, customerName);
        //  总记录数
        map.put("total", customerDao.getCustomerCount(customerName));
        map.put("rows", customerList);
        return map;
    }

    //  新增或更新
    @Override
    public ServiceVO save(Customer customer) {
        if (customer.getCustomerId() != null) {
            //  id不为空执行更新操作
            customerDao.updateCustomer(customer);
        } else {
            //  id为空执行新增方法
            customerDao.insertCustomer(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    //  删除
    @Override
    public ServiceVO delete(String ids) {
        String[] idsList = ids.split(",");
        customerDao.deleteCustomer(idsList);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
